package com.saop;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;

public class SaopDatabase {


    /**
     *
     * @param sql
     * @param param
     * @param state  1-isale 2-extCustOrderId
     * @return
     */

    public String getSaopDatabase(String sql,String param,int state) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");

            try {
                //格式一: Oracle JDBC Thin using an SID:
                //  jdbc:oracle:thin:@host:port:SID
                // Example: jdbc:oracle:thin:@localhost:1521:orcl

//                格式二: Oracle JDBC Thin using a ServiceName:
//                jdbc:oracle:thin:@//host:port/service_name
//                        Example:jdbc:oracle:thin:@//localhost:1521/orcl.city.com

                connection = DriverManager.getConnection("jdbc:oracle:thin:@//136.192.50.109:59010/inst", "saop", "Test_567");

                preparedStatement = connection.prepareStatement(sql);

                preparedStatement.setString(1, param);

                ResultSet resultSet = preparedStatement.executeQuery();

                if(state == 1)
                    if (resultSet.next()) {
                        String ol_id = resultSet.getString("OL_ID");
                        String lan_id = resultSet.getString("LAN_ID");
                        String ext_cust_order_id = resultSet.getString("EXT_CUST_ORDER_ID");
                        return ol_id + "," + lan_id + "," + ext_cust_order_id;
                    }

                if (state == 2)
                    if(resultSet.next())
                        return resultSet.getString("OL_ID");



            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        finally {
            try {
                connection.close();
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
